package cn.easybuy.dao;

import java.io.FileInputStream;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;

public class BaseDao {
    private static Connection conn;
    private static String driver;
    private static String url;
    private static String username;
    private static String password;
    static {
        try {
            init();
        } catch (Exception e) {
        }
    }
    private static void init() throws IOException, SQLException, ClassNotFoundException {
        Properties p = new Properties();
        FileInputStream fis = new FileInputStream("D:\\merge-group-yimaicom-project\\easy_buy\\src\\resources\\jdbc.properties");
        p.load(fis);
        driver = (String)p.get("driver");
        url =(String)p.get("url");
        username=(String)p.get("username");
        password=(String)p.get("password");
        conn = getConnection();
    }
    private static Connection getConnection() throws ClassNotFoundException, SQLException {
        Class.forName(driver);
        return DriverManager.getConnection(url,username,password);
    }
    //增删改的方法
    public int executeUpdate(String sql, Object[] obj) throws SQLException {
        PreparedStatement ps = conn.prepareStatement(sql);
        if(obj!=null){
            for (int i = 0; i <obj.length ; i++) {
                ps.setObject(i+1,obj[i]);
            }
        }
        return ps.executeUpdate();

    }
    //查询的方法
    public ResultSet executeQuery(String sql,Object[] obj) throws SQLException {
        PreparedStatement ps = conn.prepareStatement(sql);
        if (obj!=null){
            for (int i = 0; i <obj.length ; i++) {
                ps.setObject(i+1,obj[i]);
            }
        }

        return ps.executeQuery();

    }
    //关闭的方法
}

